/* eslint-disable global-require */
const program = require('commander');
const path = require('path');
const { version } = require('./constants');// 获取版本号

const mapAction = {
  create: { // 创建模板
    alias: 'c', // 配置命令的别称
    description: 'create a project', // 命令相对应的描述
    examples: [
      'zhu-cli create <project-name>',
    ],
  },
  config: { // 配置文件
    alias: 'conf',
    description: 'config project variable',
    examples: [
      'zhu-cli config set <k> <v>',
      'zhu-cli config get <k>',
    ],
  },
  '*': { // 根据自己的情况配置别的命令
    alias: '',
    description: 'command not found',
    examples: [],
  },
};

// 相等于 Object.key() 循环遍历创建命令
Reflect.ownKeys(mapAction).forEach((action) => {
  program.command(action) // 配置命令的名字
    .alias(mapAction[action].alias) // 命令别的名称
    .description(mapAction[action].description) // 命令对应的描述
    .action(() => {
      if (action === '*') {
        console.log(mapAction[action].description);
      } else {
        console.log(action);
        // eslint-disable-next-line import/no-dynamic-require
        require(path.resolve(__dirname, action))(...process.argv.slice(3));
      }
    });
});

program.on('--help', () => {
  console.log('\r\nExamples:');
  Reflect.ownKeys(mapAction).forEach((action) => {
    mapAction[action].examples.forEach((example) => {
      console.log(`   ${example}`);
    });
  });
});

program.version(version).parse(process.argv);
